import time
import unittest
import utils
import requests

from api.login import LoginApi
from api.trust_account import TrustAccountApi
from bs4 import BeautifulSoup


class TestTrustRegister(unittest.TestCase):

    @classmethod
    def setUpClass(cls) -> None:
        cls.login_api = LoginApi()
        cls.trust_account = TrustAccountApi()

    def setUp(self) -> None:
        self.session = requests.Session()
        self.login_api.login(self.session, "13012345678", "test123")

    def tearDown(self) -> None:
        self.session.close()

    # 开户成功
    def test01_trust_register_success(self):
        # 调用接口
        r = self.trust_account.trust_register(self.session)
        print("trust_register text=", r.text)
        print("trust_register data=", r.json())

        # 断言
        utils.common_assert(self, r, 200, 200, None)

        # 提取表单数据
        html = r.json().get("description").get("form")
        print("html==", html)

        # 使用bs4提取第三方接口数据
        # soup = BeautifulSoup(html, "html.parser")
        # action = soup.form.get("action")
        # print("action=", action)
        # form_data = {}
        # for input in soup.find_all("input"):
        #     name = input.get("name")
        #     value = input.get("value")
        #     print(f"name={name}  value={value}")
        #     form_data[name] = value
        # print("form_data===", form_data)
        action, form_data = utils.parse_html(html)

        # 调用第三方开户接口
        response = requests.post(action, data=form_data)
        self.assertEqual(200, response.status_code)
        print("第三方开户接口返回结果：", response.text)
